<template>
  <div class="login-container">
    <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">登录系统</h3>
      </div>

      <el-form-item prop="username" style="width: 100%;">
        <el-input
          ref="usernameRef"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        >
          <template #prefix>
            <el-icon class="el-input__icon"><user /></el-icon>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item prop="password" style="width: 100%;">
        <el-input
          ref="passwordRef"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          autocomplete="on"
          @keyup.enter="handleLogin"
        >
          <template #prefix>
            <el-icon class="el-input__icon"><lock /></el-icon>
          </template>
          <template #suffix>
            <el-icon class="el-input__icon" @click="showPwd">
              <view v-if="passwordType === 'password'" />
              <hide v-else />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%; margin-bottom:30px;" @click.prevent="handleLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { User, Lock, View, Hide } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/user'
import { ElMessage } from 'element-plus'

export default {
  name: 'Login',
  components: {
    User,
    Lock,
    View,
    Hide
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码不能小于3位'))
      } else {
        callback()
      }
    }
    
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.passwordRef.focus()
      })
    },
    handleLogin() {
      if (this.$refs.loginFormRef) {
        this.$refs.loginFormRef.validate(valid => {
          if (valid) {
            this.loading = true
            const userStore = useUserStore()
            userStore.login(this.loginForm)
              .then(() => {
                this.$router.push({ path: this.redirect || '/' })
                this.loading = false
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            ElMessage.error('请输入有效的用户名和密码')
            return false
          }
        })
      } else {
        // 如果没有找到表单引用，直接尝试登录
        this.loading = true
        const userStore = useUserStore()
        userStore.login(this.loginForm)
          .then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          })
          .catch(() => {
            this.loading = false
          })
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #50e3c2 0%, #1cb5e0 100%);
}

.login-form {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(28,181,224,0.15);
  padding: 40px 32px 32px 32px;
  width: 340px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-title {
  color: #1cb5e0;
  font-size: 2rem;
  margin-bottom: 24px;
  font-weight: bold;
  letter-spacing: 2px;
}

.login-form .el-input {
  width: 100%;
  margin-bottom: 18px;
}

.login-btn {
  width: 100%;
  padding: 12px 0;
  background: linear-gradient(90deg, #50e3c2 0%, #1cb5e0 100%);
  color: #fff;
  font-size: 1.1rem;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: bold;
  letter-spacing: 1px;
  transition: background 0.2s;
}

.login-btn:hover {
  background: linear-gradient(90deg, #1cb5e0 0%, #50e3c2 100%);
}
</style>